<!--头部公共部分-->
{include file="common/header" /}
<body class="layui-anim layui-anim-up">
<div class="x-nav">
      <span class="layui-breadcrumb">
        <a>首页</a>
        <a>设置</a>
        <a>字典管理</a>
        <a><cite>类型列表</cite></a>
      </span>
    <a class="layui-btn layui-btn-xs layui-btn-small" style="float:right;margin-top: 10px;"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon layui-icon-refresh"></i> </a>
</div>
<div style="padding:15px;">
    <form class="layui-form layui-form-pane">
        <div class="layui-form-item">
            <label class="layui-form-label"><span class="x-red">*</span>类型名称</label>
            <div class="layui-input-inline">
                <input type="text" name="title" required lay-verify="required" autocomplete="off" placeholder="请输类型名称" class="layui-input">
            </div>
            <div class="layui-input-inline">
                <input class="layui-btn layui-bg-blue" lay-submit lay-filter="form" value="新增类型" readonly>
            </div>
            <div class="layui-input-inline">
                <a class="layui-btn layui-btn-small" style="margin-left: 20px;" href="{:url('Config/dictionary')}"
                   title="返回字典管理">返回字典管理</a>
            </div>
        </div>
    </form>

</div>
<div id="wodekehu" class="x-body" class="clear:both;">
    <table class="layui-table" id="datalist" lay-filter="Event"></table>
    <script type="text/html" id="sortTable">
        <input class="layui-input" name="sort" style="height: 28px;" required="" lay-verify="required" placeholder="" autocomplete="off" type="text" id="change_sort_{{d.id}}" value="{{d.sort}}" data-url="/index.php/admin/config/change_sort/id/{{d.id}}.html" onchange="change_sort({{d.id}})">
    </script>
</div>
<!--底部公共部分-->
{include file="common/footer" /}

<script>
    //修改排序值
    function change_sort(id){
        var sort = $("#change_sort_"+id).val();
        var _url = $("#change_sort_"+id).attr("data-url");
        $.ajax({
            type:'POST',
            url:_url,
            dataType: "json",
            data:{"sort":sort},
            success:function(result){
                console.log(result);
                if (result.code === 0) {
                    setTimeout(function () {
                        location.href = result.url;
                    }, 1000);
                }
                layer.msg(result.msg);
            }
        })
    }
    //JavaScript代码区域
    layui.use(['table','element', 'form', 'layer', 'laydate'], function () {
        var $ = layui.$,
            element = layui.element,
            form = layui.form,
            table = layui.table,
            layer = layui.layer;

        //方法级渲染
        var tableIns = table.render({
            elem: '#datalist',
            url: '{:url("config/type")}',
            method: 'POST',
            cols: [[
                {field: 'id', title: 'ID', width: 100, sort: true, fixed: false , align: 'center'},
                {field: 'title', title: '类型名称', minWidth: 200 , align: 'center',edit: 'text'},
                {field: 'sort', title: '排序值', width: 100 , align: 'center',edit: 'text',toolbar: '#sortTable'},
                {field: 'create_time', title: '创建时间', width: 300 , align: 'center'},
                {field: 'update_time', title: '更新时间', width: 300 , align: 'center'},
            ]],
            limit: 5,
            page: true,
            height: 'full-234',
            done: function (res, curr, count) {
                //如果是异步请求数据方式，res即为你接口返回的信息。
                //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
            }
        });

        table.on('edit(Event)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field; //得到字段
            if(field == 'title'){
                $.post('/admin/config/type_add',{'id':data.id,'title':value},function(result){
                    layer.msg(result.msg);
                    if(result.code === 0){
                        setTimeout(function () {
                            location.reload();
                        }, 2000);
                    }
                });
            }
        });

        //监听提交
        form.on('submit(form)', function (data) {
            var field = data.field;
            var isLoad;
            $.ajax({
                url: "{:url('config/type_add')}",
                method: "post",
                data: field,
                beforeSend: function () {
                    isLoad = layer.load(0, {
                        shade: false
                    }); //0代表加载的风格，支持0-2
                },
                success: function (result) {
                    layer.msg(result.msg);
                    if (result.code === 0) {
                        setTimeout(function () {
                           location.reload();
                        }, 2000);
                    }
                },
                complete: function () {
                    layer.close(isLoad)
                },
                error: function (error) {
                    console.log(error)
                }
            });
            return false;
        });
    });
</script>